//
//  UIAlertView+Blocks.m
//
//  Created by Alexey Aleshkov on 17.09.12.
//  Copyright (c) 2012 DzeTech. All rights reserved.
//


#import "UIAlertView+Blocks.h"
#import <objc/runtime.h>


static NSString *const kUIAlertViewCancelBlock = @"UIAlertView.CancelBlock";
static NSString *const kUIAlertViewDismissBlock = @"UIAlertView.DismissBlock";


@implementation UIAlertView (Blocks)

+ (void)showWithTitle:(NSString *)title message:(NSString *)message cancelBlock:(UIAlertViewCancelBlock)cancelBlock dismissBlock:(UIAlertViewDismissBlock)dismissBlock cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION
{
	UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];

	alertView.delegate = alertView;
	objc_setAssociatedObject(alertView, kUIAlertViewCancelBlock, cancelBlock, OBJC_ASSOCIATION_COPY);
	objc_setAssociatedObject(alertView, kUIAlertViewDismissBlock, dismissBlock, OBJC_ASSOCIATION_COPY);
	
	if (cancelButtonTitle) {
		[alertView addButtonWithTitle:cancelButtonTitle];
		alertView.cancelButtonIndex = [alertView numberOfButtons] - 1;
	}
	
	id eachObject;
	va_list argumentList;
	if (otherButtonTitles) {
		[alertView addButtonWithTitle:otherButtonTitles];
		va_start(argumentList, otherButtonTitles);
		while ((eachObject = va_arg(argumentList, id))) {
			[alertView addButtonWithTitle:eachObject];
		}
		va_end(argumentList);
	}
	
	[alertView show];
	[alertView release];
}

#pragma mark -
#pragma mark Init / Dealloc

- (id)initWithTitle:(NSString *)title message:(NSString *)message cancelBlock:(UIAlertViewCancelBlock)cancelBlock dismissBlock:(UIAlertViewDismissBlock)dismissBlock cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION
{
	objc_setAssociatedObject(self, kUIAlertViewCancelBlock, cancelBlock, OBJC_ASSOCIATION_COPY);
	objc_setAssociatedObject(self, kUIAlertViewDismissBlock, dismissBlock, OBJC_ASSOCIATION_COPY);

	self = [self initWithTitle:title message:message delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
	if (!self) {
		return nil;
	}

	if (cancelButtonTitle) {
		[self addButtonWithTitle:cancelButtonTitle];
		self.cancelButtonIndex = [self numberOfButtons] - 1;
	}
	
	id eachObject;
	va_list argumentList;
	if (otherButtonTitles) {
		[self addButtonWithTitle:otherButtonTitles];
		va_start(argumentList, otherButtonTitles);
		while ((eachObject = va_arg(argumentList, id))) {
			[self addButtonWithTitle:eachObject];
		}
		va_end(argumentList);
	}

	return self;
}

#pragma mark -
#pragma mark UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	UIAlertViewDismissBlock dismissBlock = objc_getAssociatedObject(self, kUIAlertViewDismissBlock);
	if (dismissBlock) {
		dismissBlock(buttonIndex);
	}
}

- (void)alertViewCancel:(UIAlertView *)alertView
{
	UIAlertViewCancelBlock cancelBlock = objc_getAssociatedObject(self, kUIAlertViewCancelBlock);
	if (cancelBlock) {
		cancelBlock();
	}
}

@end
